'use strict';

const Service = require('egg').Service;

const {
  SITE_NAVIGATION_CATCE_KEY,
  SITE_NAVIGATION_GROUP_CATCE_KEY
} = require('../../config/redisKey');


class SiteNavigation extends Service {

  /**
   * 创建
   */
  async create() {
    this.ctx.clearCache(SITE_NAVIGATION_CATCE_KEY, SITE_NAVIGATION_GROUP_CATCE_KEY)
    const { SiteNavigation } = this.ctx.model;
    const model = { ...this.ctx.w_body('section title url') };
    model.date = new Date();
    return await new SiteNavigation(model).save();
  }

  /**
   * 更新数据
   */
  async update() {
    this.ctx.clearCache(SITE_NAVIGATION_CATCE_KEY, SITE_NAVIGATION_GROUP_CATCE_KEY)
    const { SiteNavigation } = this.ctx.model;
    const { id } = this.ctx.params
    const model = { ...this.ctx.w_body('section title url') };
    model.date = new Date();
    return await SiteNavigation.updateModel(id, model)
  }

  /**
   * 查询类型
   */
  async findSections() {
    return await this.ctx.getCache(SITE_NAVIGATION_GROUP_CATCE_KEY, async () => {
      const { SiteNavigation } = this.ctx.model;
      return await SiteNavigation.findSections()
    })
  }

  /**
   * 获取所有数据
   */
  async all() {
    const { SiteNavigation } = this.ctx.model;
    return await SiteNavigation.all()
  }

  /**
   * 删除数据
   */
  async destroy(id) {
    this.ctx.clearCache(SITE_NAVIGATION_CATCE_KEY, SITE_NAVIGATION_GROUP_CATCE_KEY)
    const { SiteNavigation } = this.ctx.model;
    return await SiteNavigation.removeModel(id)
  }
}

module.exports = SiteNavigation;
